package util.preprocessing;

import java.sql.Date;
import java.sql.Timestamp;
import java.util.HashSet;
import java.util.Hashtable;


import util.io.FileInput;





public class sessionExtraction {

	/**
	 * @param args
	 */

	
	
	public static long window=(long) 1800;
	
	public static String session(FileInput in, String ini) {

		
		return null;
	}
	
		
	public static void extractSessions(String aolPath, String dmoz_kt_path,
			boolean onlyKids) {

		FileInput in = new FileInput(aolPath);

		String ini = in.readString();
		ini = in.readString();
		long sessionId = 0;

		Hashtable<String, Integer[]> hash = null;

		if (onlyKids) {
			hash = dmozExtractor.genKT_URLandType(dmoz_kt_path);
		}

		while (!in.eof()) {

			String userIni = "";

			boolean sessionOn = true;
			boolean startsWithKids = false;
			boolean containsKids = false;

			String session = sessionId + "\t" + ini;

			String tmp = null;

			tmp = in.readString();
			if(tmp!=null && tmp.startsWith("AnonID"))
				tmp=in.readString();

			String field_init[] = ini.trim().split("\t");

			Timestamp init = java.sql.Timestamp.valueOf(field_init[2]);
			userIni = field_init[0];

			if (hash != null
					&& evaluateDomainInDmoz(field_init, hash, true, false,
							false)) {

				startsWithKids = true;

			}

			long previous = init.getTime() / 1000;

			while (sessionOn && tmp != null ) {

				String fields[] = tmp.trim().split("\t");

				Timestamp time = java.sql.Timestamp.valueOf(fields[2].trim());
				String userCurrent = fields[0].trim();

				long current = time.getTime() / 1000;

				if (hash != null
						&& evaluateDomainInDmoz(fields, hash, true, false,
								false)) {

					containsKids = true;

				}

				if (userCurrent.trim().equals(userIni.trim())) {

					if ((current - previous) <= window) {

						session += "\n" + sessionId + "\t" + tmp;
						tmp = in.readString();
						
						if(tmp.startsWith("AnonID"))
							tmp=in.readString();
					} else {
						ini = tmp;
						sessionOn = false;
						sessionId++;
					}

				} else {

					ini = tmp;
					sessionOn = false;
					sessionId++;

				}

			}

			if (onlyKids) {

				if (containsKids || startsWithKids) {

					System.out.println(session);
				}

			} else {
				System.out.println(session);

			}
		}

	}
	
	
	
	public static void extractSessions_2(String aolPath, String dmoz_kt_path,
			boolean onlyKids) {

		FileInput in = new FileInput(aolPath);

		String ini = in.readString();
		while (ini.startsWith("AnonID"))
			ini = in.readString();

		long sessionId = 0;

		Hashtable<String, Integer[]> hash = null;

		if (onlyKids) {
			hash = dmozExtractor.genKT_URLandType(dmoz_kt_path);
		}

		while (!in.eof()) {

			String userIni = "";

			boolean sessionOn = true;
			boolean startsWithKids = false;
			boolean containsKids = false;

			String session = sessionId + "\t" + ini;

			String tmp = null;

			tmp = in.readString();

			while (tmp.startsWith("AnonID"))
				tmp = in.readString();

			String field_init[] = ini.trim().split("\t");

			Timestamp init = java.sql.Timestamp.valueOf(field_init[2]);
			userIni = field_init[0];

			if (hash != null
					&& evaluateDomainInDmoz(field_init, hash, true, false,
							false)) {

				startsWithKids = true;
			}

			long previous = init.getTime() / 1000;

			while (sessionOn && tmp != null) {

				String fields[] = tmp.trim().split("\t");

				Timestamp time = java.sql.Timestamp.valueOf(fields[2].trim());
				String userCurrent = fields[0].trim();

				long current = time.getTime() / 1000;

				if (hash != null
						&& evaluateDomainInDmoz(fields, hash, true, false,
								false)) {

					containsKids = true;

				}

				if (userCurrent.trim().equals(userIni.trim())) {
					if ((current - previous) < window) {

						session += "\n" + sessionId + "\t" + tmp;
						userIni = fields[0].trim();
						init = java.sql.Timestamp.valueOf(fields[2].trim());
						previous = init.getTime() / 1000;
						tmp = in.readString();

						while (tmp.startsWith("AnonID"))
							tmp = in.readString();

					} else {

						ini = tmp;
						sessionOn = false;
						sessionId++;
					}

				} else {

					ini = tmp;
					sessionOn = false;
					sessionId++;

				}

			}

			if (onlyKids) {

				if (containsKids || startsWithKids) {

					System.out.println(session);
				}

			} else {
				System.out.println(session);

			}
		}

	}

	private static boolean evaluateDomainInDmoz(String host[],
			Hashtable<String, Integer[]> hash, boolean kids, boolean teens,
			boolean mteens) {

		if (host.length > 4) {

			if (hash.containsKey(host[4].trim())) {

				Integer i[] = hash.get(host[4].trim());

				if (aolQueryLogExtractor.satisfyMode(i, kids, teens, mteens)) {
					return true;
				}
			}
		}
		return false;

	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		String aol = "/home/sergio/projects/data/AOL_queryLOG/allAOL.txt";
		String path = "/home/sergio/projects/data/dmoz/kt/kt-content.rdf.u8";
		
		extractSessions_2(aol,path,false);
	}

}
